<?php
class ControllerContentContent extends Controller {
	private $error = array();
	
	public function index() {
		$this->load->language('content/content');

		$this->document->title = $this->language->get('heading_title');

		$this->load->model('content/content');
		
		$this->load->model('menu/menu');

		$this->getList();
	}
	
	public function insert() {
		$this->load->language('content/content');

		$this->document->title = $this->language->get('heading_title');

		$this->load->model('content/content');

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			//upload pic
			$uploaddir = DIR_IMAGE . 'data';
			if(!is_dir($uploaddir)) {
				mkdir($uploaddir,0777);
			}
			$uploadfile = $uploaddir .'/'.$_FILES['image']['name'];
			if (move_uploaded_file($_FILES['image']['tmp_name'], $uploadfile)) {
				$uploadfile = 'data/'.$_FILES['image']['name'];
				$this->model_content_content->addContent($this->request->post, $uploadfile);
			} else {
				$this->model_content_content->addContent($this->request->post, "");
			}
			
			
			$this->session->data['success'] = $this->language->get('text_success');
			
			$url = '';

			if (isset($this->request->get['menu_id'])) {
				$url .= '&menu_id=' . $this->request->get['menu_id'];
			}
		
			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}

			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}
			
			$this->redirect(HTTPS_SERVER . 'index.php?route=content/content&token=' . $this->session->data['token'] . $url);
    	}
    	
		$this->getForm();
	}
	
	public function update() {
		$this->load->language('content/content');

		$this->document->title = $this->language->get('heading_title');

		$this->load->model('content/content');
		
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			//upload pic
			$uploaddir = DIR_IMAGE . 'data';
			if(!is_dir($uploaddir)) {
				mkdir($uploaddir,0777);
			}
			$uploadfile = $uploaddir .'/'.$_FILES['image']['name'];
			if (move_uploaded_file($_FILES['image']['tmp_name'], $uploadfile)) {
				$uploadfile = 'data/'.$_FILES['image']['name'];
				$this->model_content_content->editContent($this->request->get['content_id'], $this->request->post, $uploadfile);
			} else {
				$this->model_content_content->editContent($this->request->get['content_id'], $this->request->post, "");
			}
			
			
			$this->session->data['success'] = $this->language->get('text_success');
			
			$url = '';
			
			if (isset($this->request->get['menu_id'])) {
				$url .= '&menu_id=' . $this->request->get['menu_id'];
			}
			
			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}
			
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}
			
			$this->redirect(HTTPS_SERVER . 'index.php?route=content/content&token=' . $this->session->data['token'] . $url);
    	}
    	
		$this->getForm();
	}
	
	public function delete() {
		$this->load->language('content/content');

		$this->document->title = $this->language->get('heading_title');

		$this->load->model('content/content');
		
		$this->load->model('menu/menu');
		
		if (isset($this->request->post['selected'])) {
      		foreach ($this->request->post['selected'] as $content_id) {
				$this->model_content_content->deletecontent($content_id);	
			}

			$this->session->data['success'] = $this->language->get('text_success');
			
			$url = '';

			if (isset($this->request->get['menu_id'])) {
				$url .= '&menu_id=' . $this->request->get['menu_id'];
			}
			
			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}
			
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}
			
			$this->redirect(HTTPS_SERVER . 'index.php?route=content/content&token=' . $this->session->data['token'] . $url);
    	}
    	
		$this->getList();
	}
	
	private function getList() {
		if (isset($this->request->get['menu_id'])) {
			$menu_id = $this->request->get['menu_id'];
		} else {
			$menu_id = '';
		}
		//echo $menu_id;
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}
		
		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			$sort = 'title';
		}

		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = 'ASC';
		}
		
		$url = '';

		if (isset($this->request->get['menu_id'])) {
			$url .= '&menu_id=' . $this->request->get['menu_id'];
		}
		
		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}
		
		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}
			
		$this->document->breadcrumbs = array();

		$this->document->breadcrumbs[] = array(
       		'href'      => HTTPS_SERVER . 'index.php?route=common/home&token=' . $this->session->data['token'],
       		'text'      => $this->language->get('text_home'),
      		'separator' => FALSE
		);

		$this->document->breadcrumbs[] = array(
       		'href'      => HTTPS_SERVER . 'index.php?route=menu/menu&token=' . $this->session->data['token'] . $url,
       		'text'      => $this->language->get('text_menu'),
      		'separator' => ' :: '
      	);
      	
		$this->document->breadcrumbs[] = array(
       		'href'      => HTTPS_SERVER . 'index.php?route=content/content&token=' . $this->session->data['token'] . $url,
       		'text'      => $this->language->get('heading_title'),
      		'separator' => ' :: '
      	);
      		 
      	$this->data['insert'] = HTTPS_SERVER . 'index.php?route=content/content/insert&token=' . $this->session->data['token'] . $url;
      	$this->data['delete'] = HTTPS_SERVER . 'index.php?route=content/content/delete&token=' . $this->session->data['token'] . $url;
      	
      	$this->data['contents'] = array();

      	$data = array(
			'sort'     => $sort,
			'order'    => $order,
      		'menu_id'  => $menu_id,      
      		'start'    => ($page - 1) * $this->config->get('config_admin_limit'),
			'limit'    => $this->config->get('config_admin_limit')
      	);
		
      	$this->load->model('tool/image');
      	
		$content_total = $this->model_content_content->getTotalContents($data);
		$menu_info = $this->model_menu_menu->getMenuById($menu_id);
		$menu_name = $menu_info['menuname'];
      	$results = $this->model_content_content->getContents($data);
      	 
      	foreach ($results as $result) {
      		$action = array();

      		$action[] = array(
				'text' => $this->language->get('text_edit'),
				'href' => HTTPS_SERVER . 'index.php?route=content/content/update&token=' . $this->session->data['token'] . '&content_id=' . $result['content_id'] . $url
      		);

      		if ($result['image'] && file_exists(DIR_IMAGE . $result['image'])) {
				$image = $this->model_tool_image->resize($result['image'], 40, 40);
			} else {
				$image = $this->model_tool_image->resize('no_image.jpg', 40, 40);
			}
			
      		$this->data['contents'][] = array(
				'content_id'    => $result['content_id'],
				'pid'   => $result['pid'],
      			'title'   => $result['title'],
      			'summary'   => $result['summary'],
      			'detail'   => $result['detail'],
      			'image'   => $image,
      			'hit'   => $result['hit'],
      			'ordering'   => $result['ordering'],
				'createdate' => date($this->language->get('date_format_short'), strtotime($result['createdate'])),
				'selected'   => isset($this->request->post['selected']) && in_array($result['content_id'], $this->request->post['selected']),
				'action'     => $action
      		);
      	}
      	
      	$this->data['heading_title'] = $this->language->get('heading_title');

      	$this->data['text_no_results'] = $this->language->get('text_no_results');

      	$this->data['column_no'] = $this->language->get('column_no');
      	$this->data['column_image'] = $this->language->get('column_image');
      	$this->data['column_title'] = $this->language->get('column_title');
      	$this->data['column_ordering'] = $this->language->get('column_ordering');
      	$this->data['column_add_date'] = $this->language->get('column_add_date');
      	$this->data['column_hit'] = $this->language->get('column_hit');
      	$this->data['column_content_id'] = $this->language->get('column_content_id');
      	$this->data['column_action'] = $this->language->get('column_action');
      		
      	$this->data['button_insert'] = $this->language->get('button_insert');
      	$this->data['button_delete'] = $this->language->get('button_delete');

      	
      	if (isset($this->error['warning'])) {
      		$this->data['error_warning'] = $this->error['warning'];
      	} else {
      		$this->data['error_warning'] = '';
      	}
      	
      	if (isset($this->session->data['success'])) {
      		$this->data['success'] = $this->session->data['success'];

      		unset($this->session->data['success']);
      	} else {
      		$this->data['success'] = '';
      	}
      	
      	$url = '';

      	if ($order == 'ASC') {
      		$url .= '&order=DESC';
      	} else {
      		$url .= '&order=ASC';
      	}

		if (isset($this->request->get['page'])) {
      		$url .= '&page=' . $this->request->get['page'];
      	}
      	
		if (isset($this->request->get['menu_id'])) {
			$url .= '&menu_id=' . $this->request->get['menu_id'];
		}
      	
      	$this->data['sort_title'] = HTTPS_SERVER . 'index.php?route=content/content&token=' . $this->session->data['token'] . '&sort=title' . $url;
      	$this->data['sort_ordering'] = HTTPS_SERVER . 'index.php?route=content/content&token=' . $this->session->data['token'] . '&sort=ordering' . $url;
      	$this->data['sort_createdate'] = HTTPS_SERVER . 'index.php?route=content/content&token=' . $this->session->data['token'] . '&sort=createdate' . $url;
      	$this->data['sort_hit'] = HTTPS_SERVER . 'index.php?route=content/content&token=' . $this->session->data['token'] . '&sort=hit' . $url;
      	$this->data['sort_content_id'] = HTTPS_SERVER . 'index.php?route=content/content&token=' . $this->session->data['token'] . '&sort=content_id' . $url;
      	
      	$this->data['sort'] = $sort;
      	$this->data['order'] = $order;
      	
      	$this->data['menu_name'] = $menu_name;
      	
      	$pagination = new Pagination();
      	$pagination->total = $content_total;
      	$pagination->page = $page;
      	$pagination->limit = $this->config->get('config_admin_limit');
      	$pagination->text = $this->language->get('text_pagination');
      	$pagination->url = HTTPS_SERVER . 'index.php?route=content/content&token=' . $this->session->data['token'] . $url . '&page={page}';
      	 
      	$this->data['pagination'] = $pagination->render();
      	
      	
      	$this->template = 'content/content_list.tpl';
      	$this->children = array(
			'common/header',	
			'common/footer'	
		);

		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
	}
	
	private function getForm() {
		$this->data['heading_title'] = $this->language->get('heading_title');
    	
    	$this->data['entry_title'] = $this->language->get('entry_title');
    	$this->data['entry_add_date'] = $this->language->get('entry_add_date');
    	$this->data['entry_summary'] = $this->language->get('entry_summary');
    	$this->data['entry_detail'] = $this->language->get('entry_detail');
    	$this->data['entry_image'] = $this->language->get('entry_image');
    	$this->data['entry_ordering'] = $this->language->get('entry_ordering');
		
		$this->data['button_save'] = $this->language->get('button_save');
    	$this->data['button_cancel'] = $this->language->get('button_cancel');

    	$this->data['tab_general'] = $this->language->get('tab_general');
    	
		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		
		if (isset($this->error['title'])) {
			$this->data['error_title'] = $this->error['title'];
		} else {
			$this->data['error_title'] = '';
		}

 		if (isset($this->error['add_date'])) {
			$this->data['error_add_date'] = $this->error['add_date'];
		} else {
			$this->data['error_add_date'] = '';
		}
		
 		if (isset($this->error['ordering'])) {
			$this->data['error_ordering'] = $this->error['ordering'];
		} else {
			$this->data['error_ordering'] = '';
		}
		
		$url = '';
		
		if (isset($this->request->get['menu_id'])) {
			$url .= '&menu_id=' . $this->request->get['menu_id'];
		}
			
		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}
		
		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}
		
		$this->document->addScript('view/javascript/jquery/ui/ui.datepicker.js');
		
		
    	$this->document->breadcrumbs = array();

   		$this->document->breadcrumbs[] = array(
       		'href'      => HTTPS_SERVER . 'index.php?route=common/home&token=' . $this->session->data['token'],
       		'text'      => $this->language->get('text_home'),
      		'separator' => FALSE
   		);

   		$this->document->breadcrumbs[] = array(
       		'href'      => HTTPS_SERVER . 'index.php?route=menu/menu&token=' . $this->session->data['token'] . $url,
       		'text'      => $this->language->get('text_menu'),
      		'separator' => ' :: '
      	);
      	
   		$this->document->breadcrumbs[] = array(
       		'href'      => HTTPS_SERVER . 'index.php?route=content/content&token=' . $this->session->data['token'] . $url,
       		'text'      => $this->language->get('heading_title'),
      		'separator' => ' :: '
   		);
   		
		if (!isset($this->request->get['content_id'])) {
			$this->data['action'] = HTTPS_SERVER . 'index.php?route=content/content/insert&token=' . $this->session->data['token'] . $url;
		} else {
			$this->data['action'] = HTTPS_SERVER . 'index.php?route=content/content/update&token=' . $this->session->data['token'] . '&content_id=' . $this->request->get['content_id'] . $url;
		}
		
		$this->data['cancel'] = HTTPS_SERVER . 'index.php?route=content/content&token=' . $this->session->data['token'] . $url;
		
		$this->data['token'] = $this->session->data['token'];
		
		if (isset($this->request->get['content_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
      		$content_info = $this->model_content_content->getContentById($this->request->get['content_id']);
    	}
    	
		if (isset($this->request->get['menu_id'])) {
			$this->data['menu_id'] = $this->request->get['menu_id'];
		}
    	
		if (isset($this->request->post['title'])) {
      		$this->data['title'] = $this->request->post['title'];
    	} elseif (isset($content_info)) {
			$this->data['title'] = $content_info['title'];
		} else {
      		$this->data['title'] = '';
    	}
  
    	if (isset($this->request->post['add_date'])) {
      		$this->data['add_date'] = $this->request->post['add_date'];
    	} elseif (isset($content_info)) {
			$this->data['add_date'] = $content_info['createdate'];
		} else {
      		$this->data['add_date'] = '';
    	}

    	if (isset($this->request->post['summary'])) {
      		$this->data['summary'] = $this->request->post['summary'];
    	} elseif (isset($content_info)) {
			$this->data['summary'] = $content_info['summary'];
		} else {
      		$this->data['summary'] = '';
   		}
  
    	if (isset($this->request->post['detail'])) {
      		$this->data['detail'] = $this->request->post['detail'];
    	} elseif (isset($content_info)) {
			$this->data['detail'] = $content_info['detail'];
		} else {
      		$this->data['detail'] = '';
    	}
    	
		if (isset($this->request->post['ordering'])) {
      		$this->data['ordering'] = $this->request->post['ordering'];
    	} elseif (isset($content_info)) {
			$this->data['ordering'] = $content_info['ordering'];
		} else {
      		$this->data['ordering'] = '0';
    	}
    	
		if (isset($this->request->post['image'])) {
      		$this->data['image'] = $this->request->post['image'];
    	} elseif (isset($content_info)) {
			$this->data['image'] = $content_info['image'];
		} else {
      		$this->data['image'] = '';
    	}
    	
		$this->load->model('tool/image');
		
		if (isset($content_info) && $content_info['image'] && file_exists(DIR_IMAGE . $content_info['image'])) {
			$this->data['preview'] = $this->model_tool_image->resize($content_info['image'], 100, 100);
		} else {
			$this->data['preview'] = $this->model_tool_image->resize('no_image.jpg', 100, 100);
		}
		
    	
//		$sBasePath = HTTP_SERVER . 'view/javascript/fckeditor/';
//		$oFCKeditor = new FCKeditor('detail');
//		$oFCKeditor->BasePath = $sBasePath;
//		$oFCKeditor->Value = $this->data['detail'];
//		$oFCKeditor->ToolbarSet = 'alma';
//		$oFCKeditor->Height = '400';
//		$this->data['oFCKeditor'] = $oFCKeditor;
    	
		$this->template = 'content/content_form.tpl';
		$this->children = array(
			'common/header',	
			'common/footer'	
		);
		
		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
	}
	
	private function validateForm() {
    	if (!$this->user->hasPermission('modify', 'content/content')) {
      		$this->error['warning'] = $this->language->get('error_permission');
    	}
    
    	if ((strlen(utf8_decode($this->request->post['title'])) < 1)) {
      		$this->error['title'] = $this->language->get('error_title');
    	}

    	if ((strlen(utf8_decode($this->request->post['add_date'])) < 1)) {
      		$this->error['add_date'] = $this->language->get('error_add_date');
    	}

    	if ((strlen(utf8_decode($this->request->post['ordering'])) < 1)) {
      		$this->error['ordering'] = $this->language->get('error_ordering');
    	}
	
    	if (!$this->error) {
      		return TRUE;
    	} else {
      		return FALSE;
    	}
  	}
}
?>